//时间格式转换成字符串
function Format(datetime,fmt) {
    if (parseInt(datetime)==datetime) {
        if (datetime.length==10) {
            datetime=parseInt(datetime)*1000;
        } else if(datetime.length==13) {
            datetime=parseInt(datetime);
        }
    }
    datetime=new Date(datetime);
    var o = {
        "M+" : datetime.getMonth()+1,                 //月份
        "d+" : datetime.getDate(),                    //日
        "h+" : datetime.getHours(),                   //小时
        "m+" : datetime.getMinutes(),                 //分
        "s+" : datetime.getSeconds(),                 //秒
        "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
        "S"  : datetime.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}


function AddComment() {

    var nowUrl = window.location.href;
    var sub_string1 = nowUrl.slice(37);
    var articleId = new Number(sub_string1)

    var comment=$("#add-comment-comment").val();
    $.ajax({
        url:"webapi/blog/comment/insert",
        data:{
            comment:comment,
            articleId: articleId
        }
    }).done(function () {
        LoadComment();
        LoadRecentComment();
    })
}

function LoadRecentArticle() {
    $.ajax({
        url: "webapi/blog/article/list",
    }).done(function (rs) {

        var length=rs.length;
        var html="";

        var dateEnd = new Date();

        for(var i=0;i<length;i++){
            var item=rs[i];
            //将Date的值转化成String
            var time2=Format(item.createTime,"yyyy-MM-dd")
            var dateBegin = new Date(time2.replace(/-/g, "/"));
            var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
            var dayDiff = Math.floor(dateDiff / ( 3600 * 1000));//计算出相差小时数

            html=" <div class=\"list-group-item list-item-sm\">" +
                "<a href=\"detail?titleId="+item.id+"\" class=\"btn-link\">"+item.title+"</a><" +
                "small class=\"box-block\">"+dayDiff+"小时前"+"</small>" +
                "</div>"+html;

        }
        $("#recent-article-real").html(html);
    })
}



function LoadRecentComment(){
    $.ajax({
        url: "webapi/blog/comment/list",
    }).done(function (rs) {

        var length=rs.length;
        var html="";

        var dateEnd = new Date();



        for(var i=0;i<length;i++){
            var item=rs[i];
            //将Date的值转化成String
            var time2=Format(item.createCommentTime,"yyyy-MM-dd")
            var dateBegin = new Date(time2.replace(/-/g, "/"));
            var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
            var dayDiff = Math.floor(dateDiff / ( 3600 * 1000));//计算出相差小时数

            html=" <a href=\"#\" class=\"list-group-item\">\n" +
                "                                <div class=\"media-left pos-rel\">\n" +
                "                                    <img class=\"img-circle img-xs\" src=\"img/profile-photos/"+(item.userId)%10+".jpg\" alt=\"Profile Picture\">\n" +
                "                                </div>\n" +
                "                                <div class=\"media-body\">\n" +
                "                                    <p id=\"userRecentCommentName"+item.userId+"\"class=\"mar-no text-main\">"+item.userName+"</p>\n" +
                "                                    <small>"+dayDiff+"小时前"+"</small>\n" +
                "                                    <p class=\"pad-top text-sm\">"+item.comment+"</p>\n" +
                "                                </div>"+html;

        }
        $("#recent-comment-real").html(html);
    })
}

function LoadComment() {
    $.ajax({
        url:"webapi/blog/comment/list",
    }).done(function (rs) {
        //获取当前url参数，并进行字符串处理
        var nowUrl = window.location.href;
        var sub_string1 = nowUrl.slice(37);
        var titleId = new Number(sub_string1)

        var len=rs.length;
        var html="";
        for(var i=0;i<len;i++){
            var item=rs[i];
            if(titleId==item.articleId){
                html+="<div class=\"comments media-block\">\n" +
                    "                                        <a class=\"media-left\" href=\"#\"><img class=\"img-circle img-sm\" alt=\"Profile Picture\" src=\"/img/profile-photos/"+(item.userId)%10+".jpg\"></a>\n" +
                    "                                        <div class=\"media-body\">\n" +
                    "                                            <div class=\"comment-header\">\n" +
                    "                                                <a href=\"#\" id=\"userCommentName"+item.userId+"\"class=\"media-heading box-inline text-main text-bold\">"+item.userName+"</a>\n" +
                    "                                                <p class=\"text-muted text-sm\">"+item.createCommentTime+"</p>\n" +
                    "                                            </div>\n"+
                    "                                        <p>"+item.comment+"</p>" +
                    "                                        </div>\n" +
                    "                                    </div>"
            }
        }
        $("#real-comment").html(html);
    })
}

function loadBlog(){
    $.ajax({
        url: "webapi/blog/article/list",
    }).done(function (rs) {

        //获取当前url参数，并进行字符串处理
        var nowUrl = window.location.href;
        var sub_string1 = nowUrl.slice(37);
        var titleId = new Number(sub_string1)

        var location=0;
        for(var i=0;i<rs.length;i++){
            if(rs[i].id==titleId)
                break;
            location++;
        }
        var dateEnd = new Date();
        var item=rs[location];
        //将Date的值转化成String
        var time2=Format(item.createTime,"yyyy-MM-dd")
        var dateBegin = new Date(time2.replace(/-/g, "/"));
        var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
        var html="<img class=\"img-responsive\" src=\"/img/shared-img-"+(item.id)%6+".jpg\" alt=\"Image\">"
        $("#blog-picture").html(html);
        $("#create-people").html(item.userName);
        $('#blogTitle').html(item.title);
        $('#blogContent').html(item.content);
        $('#createTime').html(dayDiff+"天前");
    })
}
$(function () {
    loadBlog();
    LoadRecentComment();
    LoadComment();
    LoadRecentArticle();
})